package com.contest.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.contest.constant.CommonsConstant;
import com.contest.entity.GamingOdds;
import com.contest.entity.UserInfo;
import com.contest.service.GamingOddsService;
import com.contest.util.IdWorker;
import com.contest.util.PageUtils;
import com.contest.util.R;
import com.contest.util.UserLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.contest.entity.GamingQuiz;
import com.contest.service.GamingQuizService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-01-15 16:49:32
 */

@Controller
@RequestMapping("/gamingQuiz")
public class GamingQuizController {

    @Autowired
    GamingQuizService gamingQuizService;
    @Autowired
    IdWorker idWorker;
    @Autowired
    GamingOddsService gamingOddsService;

    @RequestMapping("/list")
    @ResponseBody
    public PageUtils list(@RequestParam Map<String,Object> param) throws ParseException {
        int page = Integer.parseInt(param.get("page").toString());
        int rows = Integer.parseInt(param.get("rows").toString());
        Page<GamingQuiz> mPage = new Page<GamingQuiz>(page, rows);

        Wrapper<GamingQuiz> w = new EntityWrapper<>();
        String o = String.valueOf(param.get("name"));
        String begin = String.valueOf(param.get("begin"));
        String end = String.valueOf(param.get("end"));
        if(o != null && !o.equals("") && !o.equals("null")) {
            String name = String.valueOf(o);
            w.like("fd_user_name",name);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(begin != null && !begin.equals("") && !begin.equals("null") && end != null && !end.equals("") && !end.equals("null")) {
            long b = sdf.parse(begin).getTime();
            long e = sdf.parse(end).getTime();
            if (b > e) {
                return null;
            }
        }

        if(begin != null && !begin.equals("") && !begin.equals("null")) {
            w.and("DATE(fd_time)>={0}",begin);
        }

        if(end != null && !end.equals("") && !end.equals("null")) {
            w.and("DATE(fd_time)<={0}",end);
        }
        w.orderBy("fd_time",false);
        mPage = gamingQuizService.selectPage(mPage,w);
        return new PageUtils(mPage.getRecords(),mPage.getTotal());
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(GamingQuiz gamingQuiz) {
        gamingQuiz.setFdId(idWorker.nextId() + "");
        if (gamingQuizService.insert(gamingQuiz)) {
            return R.ok("操作成功!");
        } else {
            return R.error("操作失败");
        }
    }

    @GetMapping("/edit/{fdId}")
    public String edit(Model model, @PathVariable("fdId") String fdId) {
        GamingQuiz gamingQuiz = gamingQuizService.selectById(fdId);
        model.addAttribute("gamingQuiz", gamingQuiz);
        return "gamingQuiz/edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") String[] ids) {
        List<String> fdIds = new ArrayList<String>();
        for (String id : ids) {
            fdIds.add(id);
        }
        if (gamingQuizService.deleteBatchIds(fdIds)) {
            return R.ok("删除成功");
        } else {
            return R.error(-1, "删除失败!");
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(GamingQuiz gamingQuiz) {
        if (gamingQuizService.updateById(gamingQuiz)) {
            return R.ok("更新成功!");
        } else {
            return R.error(-1, "更新失败!");
        }
    }

    @PostMapping("/client/gamingQuiz")
    @ResponseBody
    public R gamingQuiz(GamingQuiz gamingQuiz, HttpServletRequest request){
        UserInfo userInfo = UserLoginUtil.getCurrentUserInfo(request);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int type = gamingQuiz.getFdType();
        EntityWrapper<GamingOdds> oddsEntityWrapper = new EntityWrapper<GamingOdds>();
        oddsEntityWrapper.addFilter("fd_type={0} and fd_is_enable={1}",type,1);
        GamingOdds odds = gamingOddsService.selectOne(oddsEntityWrapper);
        gamingQuiz.setFdId(idWorker.nextId()+"");
        gamingQuiz.setFdGameName(gamingQuiz.getFdGameName());
        gamingQuiz.setFdUserId(userInfo.getFdId());
        gamingQuiz.setFdUserName(userInfo.getFdNickname());
        gamingQuiz.setFdIsSettlement(0);
        gamingQuiz.setFdOdds(odds.getFdOdds());
        gamingQuiz.setFdTime(sdf.format(new Date()));
        if(gamingQuizService.saveGamingQuiz(gamingQuiz)){
            return R.ok(gamingQuiz);
        }else{
            return R.error("购买失败,请查看余额是否不足！");
        }
    }
    @PostMapping("/client/listGamingQuiz")
    @ResponseBody
    public R listGamingQuiz(HttpServletRequest request) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        UserInfo userInfo = UserLoginUtil.getCurrentUserInfo(request);
        Wrapper<GamingQuiz> w = new EntityWrapper();
        w.and("fd_user_id={0}",userInfo.getFdId()).orderBy("fd_time",false);
        List<GamingQuiz> quizList = gamingQuizService.selectList(w);
        for(GamingQuiz gamingQuiz : quizList){
            if(!"1".equals(String.valueOf(gamingQuiz.getFdIsSettlement()))){
                long nowtime = (new Date()).getTime();
                long begin = sdf.parse(gamingQuiz.getFdTime()).getTime();
                if(nowtime > begin) {
                    int sub = (int) (nowtime - begin) / 1000;
                    int secondtime = Integer.parseInt(gamingQuiz.getFdSecondResult());
                    if(secondtime > sub) {
                        String sed = String.valueOf(secondtime - sub);
                        gamingQuiz.setFdSecondResult(sed);
                    }else{
                        gamingQuiz.setFdSecondResult("0");
                    }
                }else{
                    gamingQuiz.setFdSecondResult("0");
                }
            }
        }
        return R.ok(quizList);
    }


}
